/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.bo.IsfdSectionBo;
import com.comac.ins.isfd.domain.bo.IsfdSectionDeleteBo;
import com.comac.ins.isfd.domain.bo.query.IsfdSectionQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdSectionResultQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdSectionCountResultObjectVo;
import com.comac.ins.isfd.domain.vo.IsfdSectionResultMainVo;
import com.comac.ins.isfd.domain.vo.IsfdSectionVo;
import com.comac.ins.isfd.factory.IsfdSectionFactory;
import com.comac.ins.isfd.service.IIsfdSectionResultService;
import com.comac.ins.isfd.service.IIsfdSectionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 型材库
 *
 * @author hxloongs
 * @date 2024-07-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/section")
public class IsfdSectionController extends BaseController {

    @Autowired
    private IsfdSectionFactory isfdSectionFactory;

    @Autowired
    private IIsfdSectionResultService iIsfdSectionResultService;

    /**
     * 生成型材编码
     */
    @SaCheckPermission("isfd:section:list")
    @PostMapping("/generateSerialNumber")
    public R<String> generateSerialNumber(@RequestBody IsfdSectionBo bo) {
        String type = bo.getType();
        IsfdSectionTypeEnum isfdSectionTypeEnum = IsfdSectionTypeEnum.forValue(type);
        if (isfdSectionTypeEnum == null) {
            throw new BaseException("型材类型不存在，请确认后重试！");
        }
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(isfdSectionTypeEnum);
        return R.ok(sectionService.generateSerialNumber(bo));
    }

    /**
     * 查询型材库计算结果列表
     */
    @SaCheckPermission("isfd:section:result:list")
    @PostMapping("/result/list")
    public TableDataInfo<IsfdSectionResultMainVo> queryResultPage(@RequestBody IsfdSectionResultQueryBo bo) {
        return iIsfdSectionResultService.queryResultPage(bo);
    }

    /**
     * 查询型材库列表
     */
    @SaCheckPermission("isfd:section:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdSectionVo> queryPage(@RequestBody IsfdSectionQueryBo bo) {
        // 如果 bo.getType() 为空，则设置为 ALL
        if (StringUtils.isBlank(bo.getType())) {
            bo.setType(IsfdSectionTypeEnum.ALL.getValue());
        }
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(IsfdSectionTypeEnum.forValue(bo.getType()));
        return sectionService.queryPage(bo);
    }

    /**
     * 查询型材库数据类型,统计算例数据数量
     */
    @SaCheckPermission("isfd:section:count")
    @GetMapping("/list/count/{field}")
    public R<List<IsfdSectionCountResultObjectVo>> listCountByListByField(@PathVariable String field,
                                                                          @RequestParam IsfdSectionTypeEnum type) {
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(type);
        List<IsfdSectionCountResultObjectVo> result = sectionService.listCountByListByField(field);
        if ("type".equals(field)) {
            for (IsfdSectionCountResultObjectVo isfdSectionCountResultObjectVo : result) {
                String title = isfdSectionCountResultObjectVo.getTitle();
                String descByValue = IsfdSectionTypeEnum.getDescByValue(title);
                isfdSectionCountResultObjectVo.setTitle(descByValue);
            }
        }
        return R.ok(result);
    }

    /**
     * 按型材库数据,统计算例数据数量
     */
    @SaCheckPermission("isfd:section:count")
    @GetMapping("/list/time/count")
    public R<List<IsfdSectionCountResultObjectVo>> listTimeCount(
            @RequestParam(value = "field", required = false) String field,
            @RequestParam(value = "type") IsfdSectionTypeEnum type,
            @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime
    ) {
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(type);
        List<IsfdSectionCountResultObjectVo> result = sectionService.listTimeCount(field, startTime, endTime);
        if ("type".equals(field)) {
            for (IsfdSectionCountResultObjectVo isfdSectionCountResultObjectVo : result) {
                String title = isfdSectionCountResultObjectVo.getTitle();
                String descByValue = IsfdSectionTypeEnum.getDescByValue(title);
                isfdSectionCountResultObjectVo.setTitle(descByValue);
            }
        }
        return R.ok(result);
    }

    /**
     * 保存型材库
     */
    @SaCheckPermission("isfd:section:save")
    @Log(title = "型材库", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public R<Void> save(@RequestBody IsfdSectionBo bo) {
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(IsfdSectionTypeEnum.forValue(bo.getType()));
        return toAjax(sectionService.save(bo));
    }

    /**
     * 删除型材库
     */
    @SaCheckPermission("isfd:section:remove")
    @Log(title = "型材库", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody IsfdSectionDeleteBo isfdSectionDeleteBo) {
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(IsfdSectionTypeEnum.forValue(isfdSectionDeleteBo.getType()));
        return toAjax(sectionService.deleteWithValidByIds(isfdSectionDeleteBo.getIds()));
    }
}
